package com.shen.nettysocketchat

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.shen.nettysocketchat.bean.SingleMessage
import com.shen.nettysocketchat.bus.LiveDataBus
import com.shen.nettysocketchat.bus.LiveDataBusUtils.BUS_TYPE_CHAT_SINGLE_MESSAGE
import com.shen.nettysocketchat.utils.CThreadPoolExecutor
import java.util.*

class MainActivity : AppCompatActivity() {

    private var mEditText: EditText? = null
    private var mTextView: TextView? = null

    var userId = "100002"                           // 用的时候 一台100001 一台100002
    var token = "token_$userId"
    var hosts = "[{\"host\":\"192.168.10.170\", \"port\":8855}]"

//    private val EVENTS = arrayOf<String>(
//        Events.CHAT_SINGLE_MESSAGE
//    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mEditText = findViewById(R.id.et_content)
        mTextView = findViewById(R.id.tv_msg)

        IMSClientBootstrap.instance.init(userId, token, hosts, 1)

//        CEventCenter.registerEventListener(this, MainActivity.EVENTS)

        LiveDataBus.with<SingleMessage>(BUS_TYPE_CHAT_SINGLE_MESSAGE).observeStick(this, {
            CThreadPoolExecutor.runOnMainThread {
                mTextView?.text = it.content
            }
        })
    }

    fun sendMsg(view: View?) {
        val message = SingleMessage()


        message.msgId = UUID.randomUUID().toString()
        message.msgType = MessageType.SINGLE_CHAT.msgType
        message.msgContentType = MessageType.MessageContentType.TEXT.msgContentType
        message.fromId = userId
        message.toId = "100001"
        message.timestamp = System.currentTimeMillis()
        message.content = mEditText?.text.toString()

        MessageProcessor.instance.sendMsg(message)
    }

}